package com.jy.rhin.infra.repo.node.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.rhin.domain.document.node.NodeAuthenticationPermissionService;
import com.jy.rhin.infra.repo.node.mapper.NodeAuthenticationMapper;
import com.jy.rhin.infra.repo.node.po.NodeAuthenticationPo;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationBaseDto;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationDto;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationPermissionRelationDto;
import com.jy.rhin.sdk.dto.node.query.NodeAuthenticationReqDto;
import com.jy.starter.repository.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2022/6/10
 * @Description 文档汇总统计查询handler
 */
@Slf4j
@Service
public class NodeAuthenticationQueryHandler {

	@Resource
	private NodeAuthenticationMapper nodeAuthenticationMapper;

	@Resource
	private NodeAuthenticationPermissionService nodeAuthenticationPermissionService;

	public com.jy.framework.base.entity.Page<NodeAuthenticationDto> getNodePage(NodeAuthenticationReqDto dto) {

		QueryWrapper<NodeAuthenticationPo> entryQueryWrapper = new QueryWrapper<>();

		entryQueryWrapper.lambda()
				.like(!StringUtils.isEmpty(dto.getNodeName()), NodeAuthenticationPo::getNodeName, dto.getNodeName())
				.like(!StringUtils.isEmpty(dto.getChangshang()), NodeAuthenticationPo::getChangshang, dto.getChangshang())
				.eq(!StringUtils.isEmpty(dto.getShenhezt()) && !"3".equals(dto.getShenhezt()), NodeAuthenticationPo::getShenhezt, dto.getShenhezt())
				.eq( NodeAuthenticationPo::getZuofeibz, "0")
				.orderByDesc(NodeAuthenticationPo::getCreatedTime);
		if("3".equals(dto.getShenhezt())){
			entryQueryWrapper.lt("end_time", LocalDateTime.now());
		}
		com.baomidou.mybatisplus.extension.plugins.pagination.Page<NodeAuthenticationPo> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(dto.getPageNo(), dto.getPageSize(), true);
		com.baomidou.mybatisplus.extension.plugins.pagination.Page<NodeAuthenticationPo> selectResult = nodeAuthenticationMapper.selectPage(page, entryQueryWrapper);
		return PageUtils.page(selectResult, this::toDto);
	}


	public NodeAuthenticationDto getNodeDetail(Long id){
		NodeAuthenticationPo nodeAuthenticationPo = nodeAuthenticationMapper.selectById(id);
		NodeAuthenticationDto nodeAuthenticationDto = toDto(nodeAuthenticationPo);
		List<NodeAuthenticationPermissionRelationDto> nodeAuthenticationPermissionRelationDto = nodeAuthenticationPermissionService.getPermissionListByNodeId(nodeAuthenticationDto.getId());
		nodeAuthenticationDto.setRelationList(nodeAuthenticationPermissionRelationDto);
		return nodeAuthenticationDto;
	}

	public List<NodeAuthenticationDto> getNodeList(String nodeName){
		List<NodeAuthenticationDto> nodeAuthenticationList = new ArrayList<>();
		QueryWrapper<NodeAuthenticationPo> entryQueryWrapper = new QueryWrapper<>();
		entryQueryWrapper.lambda()
				.like(!StringUtils.isEmpty(nodeName), NodeAuthenticationPo::getNodeName, nodeName)
				.eq( NodeAuthenticationPo::getZuofeibz, "0")
				.orderByDesc(NodeAuthenticationPo::getCreatedTime);
		List<NodeAuthenticationPo> nodeAuthenticationPoList =nodeAuthenticationMapper.selectList(entryQueryWrapper);
		List<NodeAuthenticationPermissionRelationDto> nodeAuthenticationPermissionRelationDto = nodeAuthenticationPermissionService.getPermissionListByNodeId(null);
		nodeAuthenticationPoList.forEach(v->{
			List<NodeAuthenticationPermissionRelationDto> nodePermissionList = nodeAuthenticationPermissionRelationDto.stream().filter(f->String.valueOf(f.getNodeId()).equals(String.valueOf(v.getId()))).collect(Collectors.toList());
			NodeAuthenticationDto nodeAuthenticationDto = new NodeAuthenticationDto();
			BeanUtils.copyProperties(v,nodeAuthenticationDto);
			nodeAuthenticationDto.setRelationList(nodePermissionList);
			nodeAuthenticationList.add(nodeAuthenticationDto);
		});
		return nodeAuthenticationList;
	}
	private NodeAuthenticationDto toDto(NodeAuthenticationPo po) {
		NodeAuthenticationDto dto = new NodeAuthenticationDto();
		BeanUtils.copyProperties(po, dto);
		return dto;
	}


	public NodeAuthenticationBaseDto getNodeByGY(String publicKey, String action){
		NodeAuthenticationBaseDto nodeAuthenticationBaseDto = new NodeAuthenticationBaseDto();
		if(nodeAuthenticationPermissionService.hasPermission(publicKey,action)){
			QueryWrapper<NodeAuthenticationPo> entryQueryWrapper = new QueryWrapper<>();
			entryQueryWrapper.lambda().eq(NodeAuthenticationPo::getPublicKey,publicKey);
			NodeAuthenticationPo nodeAuthenticationPo = nodeAuthenticationMapper.selectOne(entryQueryWrapper);
			if(nodeAuthenticationPo!=null){
				BeanUtils.copyProperties(nodeAuthenticationPo,nodeAuthenticationBaseDto);
			}
		}
		return nodeAuthenticationBaseDto;
	}

}
